<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ruoyi-managerapi:manufacturer:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ruoyi-managerapi:manufacturer:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ruoyi-managerapi:manufacturer:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ruoyi-managerapi:manufacturer:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="arrayManufacturer"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center" />
      <el-table-column
        label="认证名称"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="厂家名称"
        align="center"
        prop="fidResult"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="单位" align="center" prop="deptIdResult" />
      <el-table-column label="接口数量" align="center" prop="apiCount" />
      <!-- <el-table-column
        label="服务器IP"
        align="center"
        prop="apiUrl"
        :show-overflow-tooltip="true"
      /> -->
      <!-- <el-table-column
        label="验证地址"
        align="center"
        prop="tokenUrl"
        :show-overflow-tooltip="true"
      /> -->
      <el-table-column label="请求方式" align="center" prop="method" />
      <el-table-column label="连接状态" align="center" prop="stateResult">
        <template #default="scope">
          <div :style="scope.row.stateStyle">{{ scope.row.stateResult }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        width="200"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="250"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ruoyi-managerapi:manufacturer:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyi-managerapi:manufacturer:remove']"
            >删除</el-button
          >
          <el-button
            link
            type="primary"
            @click="handleTestConnection(scope.row)"
            v-hasPermi="['ruoyi-managerapi:manufacturer:test']"
            ><el-icon><Odometer /></el-icon>测试</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改接口管理厂家信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="manufacturerRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="认证名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入认证名称" />
        </el-form-item>
        <el-form-item label="厂家名称" prop="fid">
          <el-select v-model="form.fid" placeholder="请选择厂家名称" filterable>
            <el-option
              v-for="item in optionsFactory"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单位" prop="deptId">
          <el-tree-select
            v-model="form.deptId"
            :data="deptOptions"
            :props="{
              value: 'deptId',
              label: 'deptName',
              children: 'children',
            }"
            placeholder="选择单位"
            check-strictly
          />
        </el-form-item>
        <el-form-item label="参数" prop="tokenJson">
          <el-input
            v-model="form.tokenJson"
            type="textarea"
            placeholder="请输入参数"
          />
        </el-form-item>
        <el-form-item label="服务器" prop="apiUrl">
          <el-input
            v-model="form.apiUrl"
            type="textarea"
            placeholder="请输入服务器IP地址"
          />
        </el-form-item>
        <el-form-item label="是否验证" prop="ifCheck">
          <el-select
            v-model="form.ifCheck"
            placeholder="请选择是否验证"
            filterable
          >
            <el-option
              v-for="item in optionsIfCheck"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="验证地址" prop="tokenUrl">
          <el-input v-model="form.tokenUrl" placeholder="请输入验证地址" />
        </el-form-item>
        <!-- <el-form-item label="解析结构" prop="mapDat">
          <el-input v-model="form.mapDat" placeholder="请输入解析结构" />
        </el-form-item>
        <el-form-item label="解析头" prop="headerName">
          <el-input v-model="form.headerName" placeholder="请输入解析头" />
        </el-form-item> -->
        <el-form-item label="请求方式" prop="method">
          <el-select
            v-model="form.method"
            placeholder="请选择请求方式"
            filterable
          >
            <el-option
              v-for="item in optionsRequest"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Manufacturer">
import { listFactoryManagement } from "@/api/baseInfo/factoryManagement";
import { listDept } from "@/api/system/dept";
import { ElMessage } from "element-plus";

onMounted(() => {
  getFactoryManagerList();
});

function getoptionsFactory(tempList) {
  optionsFactory.value = [];
  if (tempList != null) {
    tempList.forEach((item) => {
      optionsFactory.value.push({
        label: item.name,
        value: item.id,
      });
    });
  }
}

const optionsFactory = ref([]);
const arrayFactory = ref([]);
function getFactoryManagerList() {
  let queryParams1 = {
    pageNum: 1,
    pageSize: 10000,
  };
  listFactoryManagement(queryParams1).then((response) => {
    console.log(response);
    arrayFactory.value = response.rows;
    getoptionsFactory(response.rows);
    getTreeselect();
  });
}

const optionsIfCheck = ref([
  {
    label: "是",
    value: 0,
  },
  {
    label: "否",
    value: 1,
  },
]);

const optionsRequest = ref([
  {
    label: "POST",
    value: "POST",
  },
  {
    label: "GET",
    value: "GET",
  },
  {
    label: "PUT",
    value: "PUT",
  },
  {
    label: "DELETE",
    value: "DELETE",
  },
]);

const deptOptions = ref([]);
const arrayDept = ref([]);
function getTreeselect() {
  let queryParams1 = {
    pageNum: 1,
    pageSize: 10000,
  };
  listDept(queryParams1).then((response) => {
    deptOptions.value = [];
    arrayDept.value = response.data;
    arrayDept.value.forEach((item1) => {
      item1.deptId = Number(item1.deptId);
    });
    deptOptions.value = proxy.handleTree(arrayDept.value, "deptId");
    console.log("树", deptOptions.value);
    getList();
  });
}

function handleTestConnection(row) {
  testConnection(row.id).then((response) => {
    if (response.data == true) {
      ElMessage({
        type: "success",
        message: "测试连接--连接成功!",
        showClose: true,
      });
      row.state = 1;
    } else {
      ElMessage({
        message: "测试连接--连接失败!",
        type: "error",
        showClose: true,
      });
      row.state = 0;
    }
  });
  console.log(row);
  if (row.id != null) {
    updateManufacturer(row).then((response) => {
      getList();
    });
  }
}

import {
  listManufacturer,
  getManufacturer,
  delManufacturer,
  addManufacturer,
  updateManufacturer,
  testConnection,
} from "@/api/ruoyi-managerapi/manufacturer";
import { onMounted } from "vue";

const { proxy } = getCurrentInstance();

const arrayManufacturer = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询接口管理厂家信息列表 */
function getList() {
  loading.value = true;
  listManufacturer(queryParams.value).then((response) => {
    arrayManufacturer.value = response.rows;
    total.value = response.total;
    loading.value = false;

    arrayManufacturer.value.forEach((item1) => {
      item1.deptId = String(item1.deptId);
      arrayDept.value.forEach((item2) => {
        if (item1.deptId == item2.deptId) {
          item1.deptIdResult = item2.deptName;
        }
      });
      if (item1.state == 1) {
        item1.stateResult = "正常";
        item1.stateStyle = {
          color: "green",
        };
      } else {
        item1.stateResult = "断开";
        item1.stateStyle = {
          color: "red",
        };
      }
      arrayFactory.value.forEach((item2) => {
        if (item1.fid == item2.id) {
          item1.fidResult = item2.name;
        }
      });
    });

    console.log(arrayManufacturer.value);
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    secret: null,
    appKey: null,
    contact: null,
    brand: null,
    senceId: null,
    tokenJson: null,
    apiUrl: null,
    phone: null,
    headers: null,
    ifCheck: null,
    tokenUrl: null,
    mapDat: null,
    headerName: null,
    method: null,
    restType: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    state: null,
    delFlag: null,
  };
  proxy.resetForm("manufacturerRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加接口管理厂家信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getManufacturer(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改接口管理厂家信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["manufacturerRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateManufacturer(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addManufacturer(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除接口管理厂家信息编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delManufacturer(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "ruoyi-managerapi/manufacturer/export",
    {
      ...queryParams.value,
    },
    `manufacturer_${new Date().getTime()}.xlsx`
  );
}
</script>
